//
//  BaseViewController.m
//  JHJ
//
//  Created by QuasimodoHe on 2019/2/11.
//  Copyright © 2019年 hq. All rights reserved.
//

#import "BaseViewController.h"
#import "NewHomeViewController.h"
#import "OnlineEducationViewController.h"
#import "BibleViewController.h"
#import "SUPlayerManager.h"
@interface BaseViewController ()<UINavigationBarDelegate>

@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    if ([SUPlayerManager manager].currentSong) {
        CGFloat height = 0;
        if (@available(iOS 13.0, *)) {
            NSSet *set = [UIApplication sharedApplication].connectedScenes;
            UIWindowScene *windowScene = [set anyObject];
            UIWindow *window = windowScene.windows.firstObject;
            height = window.safeAreaInsets.bottom;
        } else if (@available(iOS 11.0, *)) {
            UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
            height = window.safeAreaInsets.bottom;
        }
        height += 49;

        if ([self isKindOfClass:[NewHomeViewController class]] || [self isKindOfClass:[OnlineEducationViewController class]] || [self isKindOfClass:[BibleViewController class]]) {

            if ([self isKindOfClass:[NewHomeViewController class]]) {
                NewHomeViewController *vc = (NewHomeViewController *)self;
                if (!vc.isHomePage) {
                    [[SUPlayerManager manager].bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.left.right.equalTo([UIApplication sharedApplication].keyWindow);
                        make.bottom.equalTo([UIApplication sharedApplication].keyWindow).offset(-height+49);
                        make.height.mas_equalTo(60);
                    }];
                    return;
                }
            }
            
            [[SUPlayerManager manager].bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo([UIApplication sharedApplication].keyWindow);
                make.bottom.equalTo([UIApplication sharedApplication].keyWindow).offset(-height);
                make.height.mas_equalTo(60);
            }];
        } else {
            [[SUPlayerManager manager].bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo([UIApplication sharedApplication].keyWindow);
                make.bottom.equalTo([UIApplication sharedApplication].keyWindow).offset(-height+49);
                make.height.mas_equalTo(60);
            }];
        }
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];

    if (@available(iOS 11.0, *)) self.automaticallyAdjustsScrollViewInsets = NO;
//    if (self.navigationController.viewControllers.count > 1) {
//        NSArray *targets = [self.navigationController.interactivePopGestureRecognizer valueForKey:@"targets"];
//        id target = [targets.firstObject valueForKey:@"target"];
//        SEL action = NSSelectorFromString(@"handleNavigationTransition:");
//        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
//        [self.view addGestureRecognizer:pan];
//    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)initUI{
//    NSInteger index= [[self.navigationController viewControllers]indexOfObject:self];

//    if (index > 0) {
//        [self addBackItem];
//    }
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.00];
}

- (void)removeBackButton{
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)addBackButton{
    [self.view addSubview:self.backButton];
}

- (void)goBack{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
        if (self.navigationController.viewControllers.count == 1) {
            if ([ICMusicPlayManager sharedManager].narrowView) {
                [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
            }
        }
    }
}

- (void)addBackItem{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    
    button.adjustsImageWhenHighlighted = NO;
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

    
    UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [itemView addSubview:button];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:itemView];
    
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = ({
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 10 + StatusBarHeight, 24, 24)];
            
            button.adjustsImageWhenHighlighted = NO;
            [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
            button;
        });
    }
    return _backButton;
}
@end
